﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Windows.Controls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;



namespace GetCarInfo
{
    public class CarBLL
    {
        public static List<CarInfo> GetAllCar()
        {
            List<CarInfo> list = new List<CarInfo>();
            List<CarInfo> tree = GetCarTree();
            using (WebClient wc = new WebClient())
            {
                foreach (CarInfo url in tree)
                {
                    string htmlTwo = HtmlTag.GetHtml(url.CarUrl);//第二个页面
                    List<HtmlTag> name_C = HtmlTag.FindTagByAttr(htmlTwo, "div", "class", "sortR");//车名
                    foreach (HtmlTag pageTwo in name_C)
                    {
                        string nameDd = null;
                        byte[] img = new byte[] { 0 };
                        decimal price = 0;
                        List<HtmlTag> name_two = pageTwo.FindTagByAttr("div", "class", "sortTitA");
                        if (name_two.Count > 0)
                        {
                            List<HtmlTag> name_atwo = name_two[0].FindTag("a");
                            nameDd = name_atwo[0].InnerHTML;
                        }
                        List<HtmlTag> img_two = pageTwo.FindTagByAttr("div", "class", "sortCon");
                        List<HtmlTag> img_img = img_two[0].FindTag("a");
                        if (img_img.Count > 0)
                        {
                            string imgurl = img_img[0].InnerHTML;
                            string a = HtmlTag.OneorTwo(imgurl);
                            try
                            {
                                img = wc.DownloadData(a);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.Message);
                            }
                        }
                        List<HtmlTag> price_two = pageTwo.FindTagByAttr("div", "class", "priceA");
                        List<HtmlTag> price_t = price_two[0].FindTag("strong");
                        if (price_t.Count > 0)
                        {
                            price = decimal.Parse(price_t[0].InnerHTML);
                        }
                        CarInfo newInfo = new CarInfo();
                        newInfo.car_NameA = url.car_NameA;
                        newInfo.car_NameB = url.car_NameB;
                        newInfo.car_NameC = url.car_NameC;
                        newInfo.car_NameD = url.car_NameD;
                        newInfo.car_Picture = img;
                        newInfo.car_Price = price;
                        newInfo.car_DateTime = DateTime.Now;
                        list.Add(newInfo);
                    }
                }
            }
            return list;
        }
        /*
         *获得树形菜单的值 和url
         */
        public static List<CarInfo> GetCarTree()
        {
            List<CarInfo> list = new List<CarInfo>();
            String html = HtmlTag.GetHtml("http://price.pcauto.com.cn/index/price_tree.jsp");//第一个页面
            List<HtmlTag> tags = HtmlTag.FindTagByAttr(html, "UL", "class", "tree");
            if (tags.Count > 0)
            {
                List<HtmlTag> item_tags = tags[0].FindTagByAttr("li", "class", "closeChild");
                for (int i = 0; i < item_tags.Count; i++)
                {
                    List<HtmlTag> a_Context = item_tags[i].FindTagByAttr("a", "style", "cursor:pointer;");
                    List<HtmlTag> ul_Context = item_tags[i].FindTagByAttr("UL", "class", "tree");
                    string nameAa = a_Context[0].InnerHTML;//第一个车类型
                    List<HtmlTag> b_Context = ul_Context[0].FindTag("a");
                    string nameBb = b_Context[0].InnerHTML;//第二个车类型
                    List<HtmlTag> liTag = ul_Context[0].FindTagByAttr("li", "class", "pt");
                    foreach (HtmlTag lia in liTag)
                    {
                        List<HtmlTag> a_url = lia.FindTagByAttr("a", "class", "");
                        if (a_url.Count > 0)
                        {
                            string url = a_url[0].GetAttribute("href");
                            string nameCc = a_url[0].InnerHTML;//第三个车类型
                            CarInfo car = new CarInfo();
                            car.car_NameA = nameAa;
                            car.car_NameB = nameBb;
                            car.car_NameC = nameCc;
                            car.CarUrl = url;
                            list.Add(car);
                        }
                    }
                }
            }
            return list;
        }


    }
}
